import Axios from 'axios'

const state = {
    activeName: ['first'],
    loading: [true],
	treeData: [],          
    tableData: [],          //表格数据
	expandNodeId: [],		//要展开的节点id
    row: [],                //当前操作的节点
    multipleSelection: [],
    selectRowId: [],
    rowId: null,
    label: {
        givenName: null,
        givenNumberOne: null,
        givenNumberTwo: null
    },
    names: [],      //合同编号下的名称列表
}

const mutations = {
    updateActiveName(state, name) {
        state.activeName.splice(0, 1, name);
    },
	updateExpandNodeId(state, id) {
        if(id) {
            state.expandNodeId.splice(0, 1, id);
        } else {
            state.expandNodeId.splice(0);
        }
	},
    updateRow(state, obj) {
        state.row.splice(0, 1, obj);
    },
    updateRowId(state, id) {
        state.rowId = id ? id : null;
    },  
    multipleSelection(state, arr) {
        state.multipleSelection.length = 0;
        state.selectRowId.length = 0;
        if(arr.length > 0) {
            arr.forEach(item => {
                state.multipleSelection.push(item);
                state.selectRowId.push(item['id']);
            });
        } else {
            state.multipleSelection.splice(0);
            state.selectRowId.splice(0);
        }
    },
    updateTableData(state, arr) {           //更新表格数据
        let tableData = state.tableData;
        tableData.length = 0;
        if(arr.length > 0) {
            arr.forEach(item => tableData.push(item));
        } else {
            tableData.splice(0);
        }
    }
}

const actions = {
	getTreeData({state, commit}) {
		Axios.get('auxiliary/select').then(res => {
			if(res.data.code == 20001) {
                state.loading.splice(0, 1, false);          //关闭加载动画
            	if(res.data.data) {
            		let data = res.data.data,
                        treeData = state.treeData;
                    let arr = [
                        {
                            id: 0,
                            level: 0,
                            categoryName: '车牌号码',
                            children: []
                        },
                        {
                            id: 1,
                            level: 0,
                            categoryName: '合同编号',
                            children: []
                        },
                        {
                            id: 2,
                            level: 0,
                            categoryName: '货号资料',
                            children: []
                        },
                        {
                            id: 3,
                            level: 0,
                            categoryName: '普通资料',
                            children: []
                        },
                        {
                            id: 4,
                            level: 0,
                            categoryName: '银行资料',
                            children: []
                        }
                    ];
                    treeData.splice(0, treeData.length, ...arr);
                    treeData.forEach(treeItem => {
                        treeItem['children'].splice(0);
                        data.forEach(item => {
                            item['level'] = 1;
                            if(item['dataCategoryId'] == treeItem['id']) {          //将返回的数据纳入对应的根节点下面
                                treeItem['children'].push(item);
                            }
                        });
                    });
                    //更新右侧表格数据
                    if(state.expandNodeId.length <= 0) {
                        state.row.splice(0, 1, treeData[0]);
                        state.expandNodeId.splice(0, 1, treeData[0]['id']);
                    }
                    commit('updateTableData', treeData[state.expandNodeId[0]]['children']);             //更新表格数据
            	}
            } else {
                state.loading.splice(0, 1, false);          //关闭加载动画
            }
		});
	},
    getSubClassTableData({state, commit, dispatch}) {             //通过小类id查找对应的明细数据
        let row = state.row[0],
            label = state.label,
            url = null;
        switch(row['dataCategoryId']) {
            case 0:
                url = 'auxiliaryInfoAllCategory/selectCarNumber';
                break;
            case 1:
                url = 'auxiliaryInfoAllCategory/selectContractNumber';
                switch(row['categoryName']) {
                    case '卖单合同编号':
                        label['givenName'] = '买单厂名称';
                        label['givenNumberOne'] = '卖单订单号';
                        label['givenNumberTwo'] = '买单厂订单号';
                        break;
                    case '外协厂合同编号':
                        label['givenName'] = '外协厂名称';
                        label['givenNumberOne'] = '外协订单号';
                        label['givenNumberTwo'] = '外协厂订单号';
                        break;
                    case '销售合同编号':
                        label['givenName'] = '客户名称';
                        label['givenNumberOne'] = '销售订单号';
                        label['givenNumberTwo'] = '客户订单号';
                        dispatch('getAllNames', {url: 'client/findAllClient', flag: 2});
                        break;
                    case '采购合同编号':
                        label['givenName'] = '供应商名称';
                        label['givenNumberOne'] = '采购订单号';
                        label['givenNumberTwo'] = '供应商订单号';
                        dispatch('getAllNames', {url: 'supplier/selectTree', flag: 3});
                        break;
                    default: break;
                }
                break;
            case 2:
                url = 'auxiliaryInfoAllCategory/selectGoodsInfo';
                break;
            case 3:
                url = 'auxiliaryInfoAllCategory/selectOrdinary';
                break;
            case 4:
                url = 'auxiliaryInfoAllCategory/selectBank';
                break;
            default: break;
        }
        Axios({
            url: url,
            method: 'get',
            params: {infoId: row.id}
        }).then(res => {
            if(res.data.code == 20001) {
                if(res.data.data) {
                    commit('updateTableData', res.data.data);
                }
            }
        });
    },
    getAllNames({state}, obj) {             //获取合同编号下面的各个名称列表
        Axios.get(obj['url']).then(res => {
            if(res.data.code == 20001) {
                if(res.data.data) {
                    let data = res.data.data;
                    state.names.splice(0);
                    data.forEach(item => {
                        if(obj['flag'] == 2) {
                            item['id'] = item['id'].toString();
                            state.names.push(item);
                        }
                        if(obj['flag'] == 3) {
                            item['suppliers'].forEach(item2 => {
                                item2['id'] = item2['id'].toString();
                                state.names.push(item2);
                            });
                        }
                    });
                }
            }
        });
    }
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}